/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.taxondetails;

import java.io.File;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import hu.tetete.hibiscus.BarniTestActivity;
import hu.tetete.hibiscus.DashboardActivity;
import hu.tetete.hibiscus.R;
import hu.tetete.hibiscus.edittaxon.EditTaxonActivity;
import hu.tetete.hibiscus.model.Taxon;
import hu.tetete.hibiscus.taxontree.TaxonTreeExpandableListActivity;
import hu.tetete.hibiscus.userfile.UserfileUtil;

/**
 * TODO javadoc.
 */
public class TaxonDetailsTabActivity extends TabActivity {

    private Taxon taxon;

    /**
     * Contains the possible extra parameters what the class can get from {@link Intent}.
     */
    public enum INTENT_EXTRA {
        /** The {@link Taxon taxon}. */
        TAXON
    }

    /**
     * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.taxon_details);

    }

    /**
     * @see Activity#onStart()
     */
    @Override
    protected void onStart() {
        super.onStart();
        final TabHost tabHost = getTabHost();
        tabHost.clearAllTabs();
        final Intent incomeIntent = getIntent();
        this.taxon = (Taxon)incomeIntent.getExtras().get(INTENT_EXTRA.TAXON.name());

        final ImageView taxonImage       = (ImageView)findViewById(R.id.taxonImage);
        final TextView  taxonNameField   = (TextView)findViewById(R.id.taxonName);
        final TextView  scienceNameField = (TextView)findViewById(R.id.scienceName);
        final View      homeButton       = findViewById(R.id.homeButton);

        taxonNameField.setText(taxon.getName());
        scienceNameField.setText(taxon.getScienceName());
        homeButton.setOnClickListener(new DashboardActivity.HomeButtonListener(this));

        final File photo = UserfileUtil.getTaxonPhoto(taxon);
        if (photo != null) {
            taxonImage.setImageURI(Uri.fromFile(photo));
        }

        final Intent hierarchyIntent = new Intent(this, TaxonTreeExpandableListActivity.class);
        hierarchyIntent.putExtra(TaxonTreeExpandableListActivity.INTENT_EXTRA.TAXON.name(), this.taxon);
        final TabHost.TabSpec hierarchyTabSpec = tabHost.newTabSpec("hierarchy").setIndicator("Children").setContent(hierarchyIntent);
        tabHost.addTab(hierarchyTabSpec);

        final Intent descriptionIntent = new Intent(this, BarniTestActivity.class);
        descriptionIntent.putExtra(BarniTestActivity.TEXT_CONTENT_INTENT_EXTRA, "Test text"); //TODO replace
        final TabHost.TabSpec descriptionTabSpec = tabHost.newTabSpec("description").setIndicator("Description").setContent(descriptionIntent);
        tabHost.addTab(descriptionTabSpec);

        final Uri uri = Uri.parse("http://index.hu");
        final Intent wikiDescriptionIntent = new Intent(Intent.ACTION_VIEW, uri);
        final TabHost.TabSpec wikiDescriptionTabSpec = tabHost.newTabSpec("wiki").setIndicator("Wiki").setContent(wikiDescriptionIntent);
        tabHost.addTab(wikiDescriptionTabSpec);


//        final Intent propertyTreeIntent = new Intent(this, PropertyTreeListActivity.class);
//
//        final TabHost.TabSpec propertyTreeTabSpec = tabHost.newTabSpec("propertyTree").setIndicator("Properties").setContent(propertyTreeIntent);
//        tabHost.addTab(propertyTreeTabSpec);
        //TODO create property activity and tab


        tabHost.setCurrentTab(0);
    }

    /**
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.taxon_wrapper_menu, menu);
        /* Disables edit menu if there is no taxon - the program is at the root element */
        final MenuItem editMenuItem = menu.findItem(R.id.editTaxonMenuItem);
        editMenuItem.setEnabled(this.taxon != null);
        //TODO we should disable the add if the taxon is on the lowest level.
        return true;

    }

    /**
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case R.id.newTaxonMenuItem : {
                final Intent newTaxonIntent = new Intent(this, EditTaxonActivity.class);
                newTaxonIntent.putExtra(EditTaxonActivity.INTENT_EXTRA.PARENT_TAXON.name(), this.taxon);
                startActivity(newTaxonIntent);
                return true;
            }
            case R.id.editTaxonMenuItem : {
                final Intent newTaxonIntent = new Intent(this, EditTaxonActivity.class);
                newTaxonIntent.putExtra(EditTaxonActivity.INTENT_EXTRA.EDIT_TAXON.name(), this.taxon);
                startActivity(newTaxonIntent);
                return true;
            }
            default : {
                return super.onOptionsItemSelected(item);
            }
        }

    }

}
